#include <bits/stdc++.h>
using namespace std;
int main() {
  // stack
  // query max value
  // insert erase
  multiset<int> values;
  stack<int> stk;
  int n;
  cin >> n;
  while (n--) {
    int op, val;
    cin >> op;
    if(op == 0) {
      cin >> val;
      stk.push(val);
      values.insert(val);
    } else if (op == 1){
      if (stk.empty()) continue;
      int top = stk.top();
      stk.pop();
      values.erase(values.find(top));
    } else {
      if (values.size() == 0) {
        cout << 0 << endl;
        continue;
      }
      cout << *values.rbegin() << endl;
    }
  }
}